class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) 
    {
        int prev = -1;
        int count = 0;                 // 记录新种花的数量
        int m = flowerbed.size();      // 花坛的长度

        for (size_t i = 0 ; i < m; i++)
        {
            if (flowerbed[i] == 1)
            {
                if (prev == -1)
                {
                    count += i / 2;
                }
                else
                {
                    count += (i - prev - 2) / 2;
                }
                prev = i;
            }
        }
        if (prev < 0)
        {
            count = (m + 1) / 2;
        }
        else
        {
            count += (m - prev - 1) / 2;
        }
        return count >= n;

    }
};